﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class RoleRepository : BaseRepository
    {

        public RoleRepository()
            : base()
        {
        }
        
        public RoleRepository(SecurityApiEntities context)
            :base(context)
        {
        }

        //public IQueryable<Role> GetAllApplicationRoles(int applicationId)
        //{
        //    var query = from u in _context.Roles 
        //                where 
        //                select u;

        //    return query;
        //}

        public Role GetRoleById(int id)
        {
            Role role = _context.Roles
                    .Where(p => p.RoleId == id).SingleOrDefault();
            return role;
        }

        public IQueryable<Role> GetApplicationRoles(int applicationId)
        {
            var query = _context.Roles
                    .Where(p => p.ApplicationId == applicationId);
            return query;
        }

        public int AddRole(string roleName, int applicationId=1)
        {
            int roleId = -1;

            try
            {
                Role role = new Role();
                role.RoleName = roleName;
                role.ApplicationId = applicationId;
                _context.Roles.Add(role);
                _context.SaveChanges();
                roleId = role.RoleId;
            }
            catch (Exception ex)
            {
                roleId = -1;
            }

            return roleId;
        }

        public bool RemoveRole(string roleName, int applicationId = 1)
        {
            bool bResult = true;

            try
            {
                Role role = _context.Roles.Where(p => p.RoleName == roleName && p.ApplicationId==applicationId).SingleOrDefault();
                if (null != role)
                {
                    _context.Roles.Remove(role);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public bool RemoveRole(int roleId, int applicationId = 1)
        {
            bool bResult = true;

            try
            {
                Role role = _context.Roles.Where(p => p.RoleId == roleId && p.ApplicationId == applicationId).SingleOrDefault();
                if (null != role)
                {
                    _context.Roles.Remove(role);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

    }
}
